package layers.ants;

import layers.physical.Crossroad;
import layers.physical.Road;

public class ExplorationAntStateSearchingRoadsigns extends ExplorationAntState {

	public ExplorationAntStateSearchingRoadsigns(ExplorationAnt ant) {
		super(ant);
	}

	@Override
	protected void processTick() {
		//increase hop
		this.getAnt().decreaseRemainingHops();
		this.getAnt().increaseTraveledDistance(this.getAnt().getLengthOfLastRoad());
		if(this.getAnt().getRemainingHops() > 0)
			searchRoadsigns();
		else
			this.getAnt().die();
	}

	private void searchRoadsigns() {
		//get useful vars
		int truckId = this.getAnt().getAgent().getId();
		Crossroad crossroad = this.getAnt().getCrossroad();

		//die if an similar ant had allready been on this crossroad
		if(crossroad.containsBetterExplorationMark(truckId, this.getAnt().getTraveledDistance())){
			this.getAnt().die();
		}
		//drop mark and start following pheromones towards package if there are any
		else{
			Road road = this.getAnt().getCrossroad().getRoadWithMostPheromones();
			if(road == null){
				dropMark(truckId, crossroad);
				cloneExplorationAnt();
				this.getAnt().die();
			}else{
				followRoadSigns(road, truckId,crossroad);
				this.getAnt().setState(new ExplorationAntStateFollowingRoadsigns(getAnt()));
				this.clearAnt();
			}
		}
	}
}
